package DataStr.homework04.h2;

public class InsertionSort {
    public static void InsertionSort(int[]arr) {
        int n = arr.length;
        for(int i=1;i<n;i++){
            int key=arr[i];
            int j=i-1;
            for(;j>=0&&arr[j]>key;j--){
                arr[j+1]=arr[j];
            }
            arr[j+1]=key;
        }
    }
    public static void main(String[]args){
        int []arr={49,38,65,97,76,13,27,49};
        System.out.print("直接插入法前排序:");
        for(int num:arr){
            System.out.print(num+" ");
        }
        System.out.println();
        InsertionSort(arr);
        System.out.print("直接插入法后排序:");
        for(int num:arr){
            System.out.print(num+" ");
        }
    }
}
